package org.hongbo.springbootebsapi.entity;


import org.hongbo.springbootebsapi.jwt.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

@Component
@Entity
public class AuthUser implements UserDetails {

    @Autowired
    JwtTokenUtil jwtTokenUtil;

    private static AuthUser authUser;

    @Id
    private String userId;
    private String username;
    private String password;
    private String respId;
    private String respApplId;
    private Date lastPasswordResetDate;
    @ManyToMany(cascade = {CascadeType.REFRESH},fetch = FetchType.EAGER)
    private List<SysRole> roles;
    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public List<SysRole> getRoles() {
        return roles;
    }

    public void setRoles(List<SysRole> roles) {
        this.roles = roles;
    }

    public Date getLastPasswordResetDate() {
        return lastPasswordResetDate;
    }

    public void setLastPasswordResetDate(Date lastPasswordResetDate) {
        this.lastPasswordResetDate = lastPasswordResetDate;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> auths = new ArrayList<>();
        List<SysRole> roles = this.getRoles();
        if(roles!=null && roles.size() > 0 ){
            for (SysRole role : roles) {
                auths.add(new SimpleGrantedAuthority(role.getName()));
            }
        }
        return auths;
    }

    public String getRespId() {
        return respId;
    }

    public void setRespId(String respId) {
        this.respId = respId;
    }

    public String getRespApplId() {
        return respApplId;
    }

    public void setRespApplId(String respApplId) {
        this.respApplId = respApplId;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    public AuthUser(){

    }

    public AuthUser(String userId, String username){
        this.userId = userId;
        this.username = username;
    }

    public AuthUser(String userId, String username, String respId, String respApplId){
        this.userId = userId;
        this.username = username;
        this.respId = respId;
        this.respApplId = respApplId;
    }

    //静态方法中使用注解类
    public AuthUser(String token){
        this.userId = authUser.jwtTokenUtil.getUserIdFromToken(token);
        this.username = authUser.jwtTokenUtil.getUsernameFromToken(token);
        this.respId = authUser.jwtTokenUtil.getRespIdFromToken(token);
        this.respApplId = authUser.jwtTokenUtil.getRespApplIdFromToken(token);
    }

    @PostConstruct
    public void init(){
        authUser = this;
        authUser.jwtTokenUtil = this.jwtTokenUtil;
    }

}